//*:**************************************************************************
//*:*  GR_S2_SHADSUM.NSS
//*:**************************************************************************
//:: Summon Shadow (X0_S2_ShadSum.nss) Copyright (c) 2002 Bioware Corp.
//:: Created By: Preston Watamaniuk  Created On: Oct 26, 2001
//*:**************************************************************************
//*:* Updated On: December 3, 2007
//*:**************************************************************************

//*:**************************************************************************
//*:* Include the following files
//*:**************************************************************************
//*:* Game Libraries

//*:**********************************************
//*:* Function Libraries
#include "GR_IN_SPELLS"
#include "GR_IN_SPELLHOOK"

//*:* #include "GR_IN_ENERGY"

//*:**************************************************************************
//*:* Main function
//*:**************************************************************************
void main() {
    //*:**********************************************
    //*:* Declare major variables
    //*:**********************************************
    object  oCaster         = OBJECT_SELF;
    struct  SpellStruct spInfo = GRGetSpellStruct(GetSpellId(), oCaster);

    spInfo.iCasterLevel     = GRGetLevelByClass(CLASS_TYPE_SHADOWDANCER);

    //*:* int     iDieType          = 0;
    //*:* int     iNumDice          = 0;
    //*:* int     iBonus            = 0;
    //*:* int     iDamage           = 0;
    //*:* int     iSecDamage        = 0;
    int     iDurAmount        = spInfo.iCasterLevel;
    int     iDurType          = DUR_TYPE_HOURS;

    //*:* spInfo = GRSetSpellDamageInfo(spInfo, iDieType, iNumDice, iBonus);
    spInfo = GRSetSpellDurationInfo(spInfo, iDurAmount, iDurType);

    //*:**********************************************
    //*:* Set the info about the spell on the caster
    //*:**********************************************
    GRSetSpellInfo(spInfo, oCaster);

    //*:**********************************************
    //*:* Energy Spell Info
    //*:**********************************************
    //*:* int     iEnergyType     = GRGetEnergyDamageType(GRGetSpellEnergyDamageType(spInfo.iSpellID, oCaster));
    //*:* int     iSpellType      = GRGetEnergySpellType(iEnergyType);

    //*:* spInfo = GRReplaceEnergyType(spInfo, GRGetSpellEnergyDamageType(spInfo.iSpellID, oCaster), iSpellType);

    //*:**********************************************
    //*:* Spellcast Hook Code
    //*:**********************************************
    if(!GRSpellhookAbortSpell()) return;
    spInfo = GRGetSpellInfoFromObject(spInfo.iSpellID, oCaster);

    //*:**********************************************
    //*:* Declare Spell Specific Variables & impose limiting
    //*:**********************************************

    float   fDuration       = GRGetSpellDuration(spInfo);
    //*:* float   fDelay          = 0.0f;
    //*:* float   fRange          = FeetToMeters(15.0);

    //*:**********************************************
    //*:* Resolve Metamagic, if possible
    //*:**********************************************
    if(GRGetMetamagicUsed(spInfo.iMetamagic, METAMAGIC_EXTEND)) fDuration *= 2;
    //*:* if(GRGetMetamagicUsed(spInfo.iMetamagic, METAMAGIC_WIDEN)) fRange *= 2;
    //*:* iDamage = GRGetSpellDamageAmount(spInfo, SPELL_SAVE_NONE, oCaster, SAVING_THROW_TYPE_NONE, fDelay);
    /* if(GRGetSpellHasSecondaryDamage(spInfo)) {
        iSecDamage = GRGetSpellSecondaryDamageAmount(iDamage, spInfo, SPELL_SAVE_NONE, oCaster, SAVING_THROW_TYPE_NONE, fDelay);
        if(spInfo.iSecDmgAmountType==SECDMG_TYPE_HALF) {
            iDamage = iSecDamage;
        }
    }*/

    //*:**********************************************
    //*:* Effects
    //*:**********************************************
    effect eSummon;
    effect eTurnImmune = EffectSpellImmunity(SPELLABILITY_TURN_UNDEAD);
    effect eCommandImmune = EffectSpellImmunity(SPELL_GR_COMMAND_UNDEAD);

    effect eImmuneLink = EffectLinkEffects(eTurnImmune, eCommandImmune);

    //*:**********************************************
    //*:* Apply effects
    //*:**********************************************
    string sSummonResRef;
    if(spInfo.iCasterLevel<=5) {
        sSummonResRef = "X1_S_SHADOW";
    } else if(spInfo.iCasterLevel<=8) {
        sSummonResRef = "X1_S_SHFIEND";
    } else if(spInfo.iCasterLevel<=10) {
        sSummonResRef = "X1_S_SHADLORD";
    } else {
        if(GetHasFeat(1002, oCaster)) {// has epic shadowlord feat
            //GZ 2003-07-24: Epic shadow lord
            sSummonResRef = "x2_s_eshadlord";
        } else {
            sSummonResRef = "X1_S_SHADLORD";
        }
    }
    eSummon = EffectSummonCreature(sSummonResRef, VFX_FNF_SUMMON_UNDEAD);
    GRApplyEffectAtLocation(DURATION_TYPE_TEMPORARY, eSummon, spInfo.lTarget, fDuration);

    int i = 1;
    int bFound = FALSE;
    object oSummon = GetAssociate(ASSOCIATE_TYPE_SUMMONED, oCaster, i);
    while(GetIsObjectValid(oSummon) && !bFound) {
        if(GetResRef(oSummon)==sSummonResRef) {
            bFound = TRUE;
        } else {
            i++;
            oSummon = GetAssociate(ASSOCIATE_TYPE_SUMMONED, oCaster, i);
        }
    }
    if(GetIsObjectValid(oSummon)) {
        GRApplyEffectToObject(DURATION_TYPE_PERMANENT, eImmuneLink, oSummon);
    }

    //if(spInfo.iXPCost>0) GRApplyXPCostToCaster(spInfo.iXPCost);
    //*:**********************************************
    //*:* Remove spell info from caster
    //*:**********************************************
    GRClearSpellInfo(spInfo.iSpellID, oCaster);
}
//*:**************************************************************************
//*:**************************************************************************
